package com.andnnl.vcode;

import com.andnnl.genId.SnowflakeIdWorkerOptimize;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;
import java.util.Random;

/**
 * Created by chenss on 2021/6/23.
 */
public class JYGenCode {
    static SnowflakeIdWorkerOptimize genId = new SnowflakeIdWorkerOptimize(0, 0);

    public static void main(String[] args) throws Exception {
        int a = 0;
        int b = 1;
        int c = a / b;

//        String dir = "E:\\yingdian_vcode.train2\\";
//        String dir2 = "E:\\yingdian_vcode.test2\\";
        String dir = "/data/yingdian_vcode.train6/";
        String dir2 = "/data/yingdian_vcode.test6/";
        File dir11 = new File(dir);
        dir11.mkdirs();
        File dir22 = new File(dir2);
        dir22.mkdirs();

        for (int k = 18; k <= 21; k++) {
            for (int i = 0; i < 10000; i++) {
                createCode(dir, genId.nextId(), k);
            }
            for (int i = 0; i < 2000; i++) {
                createCode(dir2, genId.nextId(), k);
            }
        }

//        createAddAndReduceCode();
    }

    public static void createCode(String dir, long idx, int fontSize) throws Exception {

        System.out.println("生成验证码中---");
	/*
		HttpSession session = request.getSession();
		response.setContentType("image/jpeg");
		ServletOutputStream sos = response.getOutputStream();
		// 设置浏览器不要缓存此图片
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		// 创建内存图象并获得其图形上下文
		BufferedImage image = new BufferedImage(GenerateCheckCode.WIDTH,
				GenerateCheckCode.HEIGHT, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		// 产生随机的认证码
		char[] rands = GenerateCheckCode.generateCheckCode();
		// 产生图像
		GenerateCheckCode.drawBackground(g);
		GenerateCheckCode.drawRands(g, rands);
		System.out.println(new String(rands));
		// 结束图像的绘制过程，完成图像
		g.dispose();
		// 将图像输出到客户端
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ImageIO.write(image, "JPEG", bos);
		byte[] buf = bos.toByteArray();
		response.setContentLength(buf.length);
		// 下面的语句也可写成：bos.writeTo(sos);
		sos.write(buf);
		bos.close();
		sos.close();*/
        // 将当前验证码存入到Session中
//        ServletOutputStream sos = response.getOutputStream();
//        response.setHeader("Pragma","No-cache");
//        response.setHeader("Cache-Control","no-cache");
//        response.setDateHeader("Expires", 0);
        int width = 80;
        int height = 20;
        BufferedImage image = new BufferedImage(width, height, 1);
        Graphics g = image.getGraphics();
        Random random = new Random();
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman", 0, fontSize));
        g.setColor(getRandColor(160, 200));
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }


        int rand1 = random.nextInt(9);
        int operator = random.nextInt(2);
//        int operator=random.nextInt(4);
        int rand2 = random.nextInt(9);
        if (operator == 3 && rand2 == 0) {
            while ((rand2 = random.nextInt(9)) == 0) {
            }
        }
        String readStr = String.valueOf(rand1);
        String readStr2 = String.valueOf(rand1);
        int readNum = 0;
//        add, subtract, multiply and divide
        if (operator == 0) {
            readNum = rand1 + rand2;
            readStr += "+";
            readStr2 += "a";
        } else if (operator == 1) {
            readNum = rand1 - rand2;
            if (rand1 < rand2) {
                readNum = rand2 - rand1;
                readStr = String.valueOf(rand2);
                readStr2 = String.valueOf(rand2);
                rand2 = rand1;
            }
            readStr += "-";
            readStr2 += "s";
        } else if (operator == 2) {
            readNum = rand1 * rand2;
            readStr += "×";
            readStr2 += "m";
        } else if (operator == 3) {
            readNum = rand1 / rand2;
            readStr += "÷";
            readStr2 += "d";
        }
        readStr += rand2;
        readStr2 += rand2;
//        readStr+="=?";
        readStr += "=?";
        readStr2 += "=";
        for (int i = 0; i < readStr.length(); i++) {
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            g.drawString(String.valueOf(readStr.charAt(i)), 13 * i + 6, 16);
        }
        // 将认证码存入SESSION
        g.dispose();
        //ImageIO.write(image, "JPEG", response.getOutputStream());

        ImageIO.write(image, "JPEG", new File(dir + idx + "_" + readStr2 + ".jpg"));


        System.out.println(readStr + " " + readStr2 + " " + readNum);

//        MemCached.addLimitTime("CODE_"+request.getSession().getId(), readNum + "", 2 * 60 * 1000);

        //session.setAttribute("sessionCode", new String(rands));
        // 直接使用下面的代码将有问题，Session对象必须在提交响应前获得
        // request.getSession().setAttribute(CHECK_CODE_KEY,new String(rands));

    }

    /**
     * 获取加减乘图片验证码
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public static void createAddAndReduceCode() throws Exception {

        System.out.println("生成验证码中---");
        // 创建内存图象并获得其图形上下文
        BufferedImage image = new BufferedImage(GenerateCheckCode.WIDTH,
                GenerateCheckCode.HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        // 产生随机的认证码
        Map<String, Object> map = GenerateCheckCode.generateCheckCodeMap();
        String[] rands = (String[]) map.get("rands");
        // 产生图像
        GenerateCheckCode.drawBackground(g);
        GenerateCheckCode.drawRands1(g, rands);
        // 结束图像的绘制过程，完成图像
        g.dispose();
        // 将图像输出到客户端
        ImageIO.write(image, "JPEG", new File("test2.jpg"));
        // 将当前验证码存入到Session中
        //session.setAttribute("sessionCode", new String(rands));
//        MemCached.addLimitTime("PLACEORDERCODE_"+request.getSession().getId(), map.get("validateCode"), 2 * 60 * 1000);
        // 直接使用下面的代码将有问题，Session对象必须在提交响应前获得
        // request.getSession().setAttribute(CHECK_CODE_KEY,new String(rands));

    }

    public static Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255) fc = 255;
        if (bc > 255) bc = 255;
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }
}
